﻿using Repository.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Repository.Impl
{
    public class BaseRepository : IBaseRepository
    {
        private SqlSugarClient _db;
        public BaseRepository(ISqlSugarClient db)
        {
            _db = db as SqlSugarClient;
        }

        /*
        //根据特性直接CRUD
        var list=db.QueryableWithAttrWithAttr<Order>().ToList();//5.0.9.1 全自动切换库查询
        db.InsertWithAttr(list).ExecuteCommand() ;//5.0.9.1 全自动切换库插入
        db.UpdateWithAttr(list).ExecuteCommand() ;//5.0.9.1 全自动切换库更新
        db.DeleteableWithAttr(list).ExecuteCommand() ;//5.0.9.1 全自动切换库删除
         */

        public List<TEntity> SelectList<TEntity>()
        {
            var list = _db.QueryableWithAttr<TEntity>().ToList();
            return _db.QueryableWithAttr<TEntity>().ToList();
        }

        /*
        public async Task<bool> InsertData<TEntity>(TEntity model) where TEntity : class, new()
        {
            var i = await _db.Insertable(model).ExecuteCommandAsync();
            return i > 0;
        }

        public async Task<TEntity> QueryData<TEntity>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderby = null, OrderByType orderByType = OrderByType.Asc)
        {
            return await _db.QueryableWithAttr<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(orderby != null, orderby, orderByType).FirstAsync();
        }
        public async Task<List<TEntity>> QueryDataList<TEntity>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderby = null, OrderByType orderByType = OrderByType.Asc)
        {
            return await _db.QueryableWithAttr<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(orderby != null, orderby, orderByType).ToListAsync();
        }

        public async Task<bool> InsertDatas<TEntity>(List<TEntity> model) where TEntity : class, new()
        {
            var i = await _db.Insertable(model).ExecuteCommandAsync();
            return i > 0;
        }
        public async Task<string> QueryValue<TEntity>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, string>> column)
        {
            return await _db.QueryableWithAttr<TEntity>().Where(where).Select(column).FirstAsync();//查询单条
        }
        public async Task<List<string>> QueryValueGroup<TEntity>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, string>> column, Expression<Func<TEntity, object>> group, Expression<Func<TEntity, object>> orderby = null, OrderByType orderByType = OrderByType.Asc)
        {
            return await _db.QueryableWithAttr<TEntity>().OrderByIF(orderby != null, orderby, orderByType).GroupBy(group).Where(where).Select(column).ToListAsync();
        }
        public async Task<List<string>> QueryValues<TEntity>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, string>> column)
        {
            return await _db.QueryableWithAttr<TEntity>().Where(where).Select(column).ToListAsync();
        }
        public async Task<bool> UpdateData<TEntity>(TEntity model, Expression<Func<TEntity, bool>> where) where TEntity : class, new()
        {
            var i = await _db.Updateable(model).Where(where).ExecuteCommandAsync();
            return i > 0;
        }
        public async Task<bool> UpdateData<TEntity>(TEntity model, Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, string>> column) where TEntity : class, new()
        {
            //查询当前值是否存在
            var targetColumn = await QueryValue(where, column);
            if (!string.IsNullOrEmpty(targetColumn))
            {
                return await UpdateData(model, where);
            }
            else
            {
                return await InsertData(model);
            }
        }
        public async Task<bool> UpdateData<TEntity>(TEntity model, Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, object>> IgnoreColumn) where TEntity : class, new()
        {
            var i = await _db.Updateable(model).IgnoreColumns(IgnoreColumn).Where(where).ExecuteCommandAsync();
            return i > 0;
        }
        public async Task<bool> InsertData<TEntity>(List<Dictionary<string, object>> recordList, string tableName)
        {
            var i = await _db.Insertable(recordList).AS(tableName).ExecuteCommandAsync();
            return i > 0;
        }
        public async Task<bool> InsertData<TEntity>(Dictionary<string, object> record, string tableName)
        {
            var i = await _db.Insertable(record).AS(tableName).ExecuteCommandAsync();
            return i > 0;
        }
        public async Task<bool> UpdateData<TEntity>(List<Dictionary<string, object>> recordList, string tableName, string whereColumns)
        {
            var i = await _db.Updateable(recordList).AS(tableName).WhereColumns(whereColumns).ExecuteCommandAsync();
            return i > 0;
        }
        public async Task<bool> UpdateData<TEntity>(Dictionary<string, object> record, string tableName, string whereColumns)
        {
            var i = await _db.Updateable(record).AS(tableName).WhereColumns(whereColumns).ExecuteCommandAsync();
            return i > 0;
        }
        */
    }
}
